﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CUiMFAdminSetTargetLibraryDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using AxEMSTREEVIEWERLib;
using EMPAPPBASELib;
using EMPMODELLib;
using EMPTREECONTROLLib;
using EMPTYPELIBRARYLib;
using EMSTREEVIEWERLib;
using EngineeringInternalExtension;
using System;
using System.ComponentModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CUiMFAdminSetTargetLibraryDlg : TxForm
{
  private Button m_cancel;
  private Button m_ok;
  private AxVCoETVDirectAccessViewer m_directAccessViewer;
  private EmpObjectKey m_selectedKey;
  private int m_targetLibraryKey;
  private IEmpTreeControl m_treeControl;
  private IContainer components;

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiMFAdminSetTargetLibraryDlg));
    this.m_directAccessViewer = new AxVCoETVDirectAccessViewer();
    this.m_cancel = new Button();
    this.m_ok = new Button();
    ((ISupportInitialize) this.m_directAccessViewer).BeginInit();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.m_directAccessViewer, "m_directAccessViewer");
    ((Control) this.m_directAccessViewer).Name = "m_directAccessViewer";
    ((AxHost) this.m_directAccessViewer).OcxState = (AxHost.State) componentResourceManager.GetObject("m_directAccessViewer.OcxState");
    this.m_cancel.DialogResult = DialogResult.Cancel;
    componentResourceManager.ApplyResources((object) this.m_cancel, "m_cancel");
    this.m_cancel.Name = "m_cancel";
    this.m_cancel.UseVisualStyleBackColor = true;
    this.m_cancel.Click += new EventHandler(this.m_cancel_Click);
    this.m_ok.DialogResult = DialogResult.OK;
    componentResourceManager.ApplyResources((object) this.m_ok, "m_ok");
    this.m_ok.Name = "m_ok";
    this.m_ok.UseVisualStyleBackColor = true;
    this.m_ok.Click += new EventHandler(this.m_ok_Click);
    ((Form) this).AcceptButton = (IButtonControl) this.m_ok;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((Form) this).CancelButton = (IButtonControl) this.m_cancel;
    ((Control) this).Controls.Add((Control) this.m_ok);
    ((Control) this).Controls.Add((Control) this.m_cancel);
    ((Control) this).Controls.Add((Control) this.m_directAccessViewer);
    ((Form) this).FormBorderStyle = FormBorderStyle.FixedDialog;
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = "CUiMFAdminSelectLibraryDlg";
    this.SemiModal = false;
    ((Form) this).ShowIcon = false;
    ((Form) this).Closed += new EventHandler(this.CUiSelectLibraryDlg_Closed);
    ((Form) this).Load += new EventHandler(this.CUiSelectLibraryDlg_Load);
    ((ISupportInitialize) this.m_directAccessViewer).EndInit();
    ((Control) this).ResumeLayout(false);
  }

  internal CUiMFAdminSetTargetLibraryDlg()
  {
    this.InitializeComponent();
    this.m_selectedKey.objectId = 0;
    this.m_targetLibraryKey = CApMFAdminUtils.m_targetLibraryId;
  }

  public virtual void OnInitTxForm()
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    this.InitTreeControl();
    this.EnableOkButton();
    Cursor.Current = current;
  }

  private void CUiSelectLibraryDlg_Load(object sender, EventArgs e)
  {
    this.RegisterToSelectionEvents();
  }

  private void CUiSelectLibraryDlg_Closed(object sender, EventArgs e) => this.UnLoadTreeControl();

  private void m_ok_Click(object sender, EventArgs e)
  {
    CApMFAdminUtils.SaveTargetLibraryToAppData(this.m_selectedKey.objectId);
    this.m_targetLibraryKey = this.m_selectedKey.objectId;
    ((Form) this).Close();
  }

  private void m_cancel_Click(object sender, EventArgs e)
  {
    this.m_selectedKey.objectId = 0;
    ((Form) this).Close();
  }

  private void EnableOkButton()
  {
    this.m_ok.Enabled = false;
    if (((IEmpAppItemList) this.m_treeControl.Selection).Size != 1)
      return;
    this.m_selectedKey = ((IEmpAppItem) ((IEmpAppItemList) this.m_treeControl.Selection).Item(0)).Key;
    if (!this.IsResourceLibrary(this.m_selectedKey))
      return;
    this.m_ok.Enabled = true;
  }

  private void EnableOkButton(bool enable) => this.m_ok.Enabled = enable;

  private void InitTreeControl()
  {
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    EmpObjectKey empObjectKey1;
    if (this.m_targetLibraryKey == 0)
    {
      empObjectKey1.objectId = TxEngineeringDataInternal.GlobalSessionGetProjectId();
    }
    else
    {
      EmpObjectKey empObjectKey2;
      empObjectKey2.objectId = this.m_targetLibraryKey;
      EmpObjectKey[] collections = new EmpResourceLibraryClass().GetCollections(ref empContext, ref empObjectKey2);
      empObjectKey1.objectId = collections[0].objectId;
    }
    this.m_directAccessViewer.LoadExternal();
    this.m_directAccessViewer.GetTreeControl(ref this.m_treeControl);
    if (this.m_treeControl != null)
    {
      this.m_treeControl.EnableRename(false);
      this.m_treeControl.ShowColumns = false;
    }
    this.m_directAccessViewer.LoadDirectAccessTree(empContext, empObjectKey1, (EmpEnumSignType) 1);
    this.m_directAccessViewer.EnableRClickMenu(0);
    ((Control) this.m_directAccessViewer).Width = this.m_cancel.Right - ((Control) this.m_directAccessViewer).Left;
    ((Control) this.m_directAccessViewer).Height = this.m_cancel.Top - ((Control) this.m_directAccessViewer).Top - 7;
    if (this.m_targetLibraryKey == 0 || !CApMFAdminUtils.DoesTargetLibraryExistInEms() || this.m_treeControl == null)
      return;
    this.m_selectedKey.objectId = this.m_targetLibraryKey;
    EmpAppItemList empAppItemList;
    this.m_treeControl.LoadTreeItemsFromKey(ref this.m_selectedKey, ref empAppItemList);
  }

  private void UnLoadTreeControl()
  {
    this.UnRegisterFromSelectionEvents();
    this.m_directAccessViewer.Unload();
  }

  private void RegisterToSelectionEvents()
  {
    _IEmpTreeControlEvents_Event treeControl = this.m_treeControl as _IEmpTreeControlEvents_Event;
    // ISSUE: method pointer
    treeControl.OnSelectionCleared += new _IEmpTreeControlEvents_OnSelectionClearedEventHandler((object) this, (UIntPtr) __methodptr(treeControlEvents_OnSelectionCleared));
    // ISSUE: method pointer
    treeControl.OnSelectionSet += new _IEmpTreeControlEvents_OnSelectionSetEventHandler((object) this, (UIntPtr) __methodptr(treeControlEvents_OnSelectionSet));
    // ISSUE: method pointer
    treeControl.OnSelectionAdded += new _IEmpTreeControlEvents_OnSelectionAddedEventHandler((object) this, (UIntPtr) __methodptr(treeControlEvents_OnSelectionAdded));
    // ISSUE: method pointer
    treeControl.OnSelectionRemoved += new _IEmpTreeControlEvents_OnSelectionRemovedEventHandler((object) this, (UIntPtr) __methodptr(treeControlEvents_OnSelectionRemoved));
  }

  private void UnRegisterFromSelectionEvents()
  {
    _IEmpTreeControlEvents_Event treeControl = this.m_treeControl as _IEmpTreeControlEvents_Event;
    // ISSUE: method pointer
    treeControl.OnSelectionCleared -= new _IEmpTreeControlEvents_OnSelectionClearedEventHandler((object) this, (UIntPtr) __methodptr(treeControlEvents_OnSelectionCleared));
    // ISSUE: method pointer
    treeControl.OnSelectionSet -= new _IEmpTreeControlEvents_OnSelectionSetEventHandler((object) this, (UIntPtr) __methodptr(treeControlEvents_OnSelectionSet));
    // ISSUE: method pointer
    treeControl.OnSelectionAdded -= new _IEmpTreeControlEvents_OnSelectionAddedEventHandler((object) this, (UIntPtr) __methodptr(treeControlEvents_OnSelectionAdded));
    // ISSUE: method pointer
    treeControl.OnSelectionRemoved -= new _IEmpTreeControlEvents_OnSelectionRemovedEventHandler((object) this, (UIntPtr) __methodptr(treeControlEvents_OnSelectionRemoved));
  }

  private void treeControlEvents_OnSelectionCleared() => this.EnableOkButton(false);

  private void treeControlEvents_OnSelectionSet(EmpAppItemList SetItemList)
  {
    this.EnableOkButton();
  }

  private void treeControlEvents_OnSelectionAdded(EmpAppItemList AddedItemList)
  {
    this.EnableOkButton(false);
  }

  private void treeControlEvents_OnSelectionRemoved(EmpAppItemList RemovedItemList)
  {
    this.EnableOkButton();
  }

  internal EmpObjectKey GetSelectedLibrary() => this.m_selectedKey;

  private bool IsResourceLibrary(EmpObjectKey selectedKey)
  {
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    return new EmpObjectClass().IsKindOf(ref empContext, ref selectedKey, "PmResourceLibrary");
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }
}
